extends Node

signal user_submitted(text: String)
signal closed()

@onready var ui_panel := $UIPanel
@onready var label := $UIPanel/Label
@onready var input := $UIPanel/UserInput
@onready var send_button := $UIPanel/SendButton
@onready var auto_hide_timer := $Timer  # 这是场景中真正的 Timer 节点

@export var type_delay := 0.05

func _ready():
	send_button.pressed.connect(_on_submit)
	input.text_submitted.connect(_on_submit)
	auto_hide_timer.timeout.connect(_on_timer_timeout)

	#ui_panel.visible = false  # 初始隐藏
func _on_submit(text = ""):
	var content: String = input.text.strip_edges()
	if content != "":
		emit_signal("user_submitted", content)
		input.text = ""

func show_text(text: String, delay: float = type_delay):
	label.text = ""
	for c in text:
		label.text += c
		await get_tree().create_timer(delay).timeout

func show_ui():
	ui_panel.visible = true
	auto_hide_timer.start(5.0)  # 使用 Timer 节点，开始倒计时

func _on_timer_timeout():
	hide_ui()

func hide_ui():
	ui_panel.visible = false
	emit_signal("closed")

func close_now():
	auto_hide_timer.stop()
	hide_ui()
